<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选角色 -->
			<cl-filter label="角色">
				<cl-select prop="role" :options="options.role" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索用户名" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="flock_user-user" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { reactive } from "vue";

const { service } = useCool();

// 选项
const options = reactive({
	role: [
		{ label: "普通成员", value: 0 },
		{ label: "管理员", value: 1 },
		{ label: "群主", value: 2 }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		// {
		// 	label: "用户ID",
		// 	prop: "userId",
		// 	hook: "number",
		// 	component: { name: "el-input-number" },
		// 	required: true
		// },
		// {
		// 	label: "群ID",
		// 	prop: "groupId",
		// 	hook: "number",
		// 	component: { name: "el-input-number" },
		// 	required: true
		// },
		{
			label: "用户名",
			prop: "username",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{ label: "头像", prop: "avatar", component: { name: "cl-upload" } },
		{
			label: "状态",
			prop: "status",
			flex: false,
			component: { name: "cl-switch" },
			required: true
		},
		{
			label: "角色",
			prop: "role",
			component: { name: "el-radio-group", options: options.role },
			value: 0,
			required: true
		},
		{
			label: "备注",
			prop: "remark",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } }
		},
		{
			label: "活跃度",
			prop: "activity",
			component: { name: "el-slider", props: { style: { width: "200px" } } },
			required: true
		},
		{
			label: "最后活动时间",
			prop: "lastActivityTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" }
			},
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		// { label: "用户ID", prop: "userId", minWidth: 140 },
		// { label: "群ID", prop: "groupId", minWidth: 140 },
		{ label: "用户名", prop: "username", minWidth: 140 },
		{
			label: "头像",
			prop: "avatar",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{ label: "状态", prop: "status", minWidth: 100, component: { name: "cl-switch" } },
		{ label: "角色", prop: "role", dict: options.role, minWidth: 120 },
		{ label: "备注", prop: "remark", showOverflowTooltip: true, minWidth: 200 },
		// {
		// 	label: "活跃度",
		// 	prop: "activity",
		// 	minWidth: 150,
		// 	component: { name: "el-progress" }
		// },
		{
			label: "最后活动时间",
			prop: "lastActivityTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.flock_user.user
	},
	(app) => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
